package com.jiutong.meipengdai.ui.activitys;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.base.CommonAdapter;
import com.jiutong.meipengdai.base.ViewHolder;
import com.jiutong.meipengdai.entity.RepayPlanRespond;
import com.jiutong.meipengdai.http.present.CommonPresent;
import com.jiutong.meipengdai.http.subscribers.SubscriberOnNextListener;

import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by suxi on 2017/8/17.
 */

public class RepayPlanActivity extends BaseActivity {
    @Bind(R.id.rl_back)
    RelativeLayout rlBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.lv_repay_plan)
    ListView lvRepayPlan;
    private String loanId;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_repay_plan;
    }

    @Override
    protected void initDate(Bundle savedInstanceState) {
        super.initDate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        loanId = extras.getString("loanId");
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);

        tvTitle.setText("还款计划");
        getRepayPlan();
    }

    @OnClick(R.id.rl_back)
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.rl_back:
                finish();
                break;
        }
    }

    private void getRepayPlan() {

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("loanId", loanId);
        CommonPresent.getRepayPlan(mContext, new SubscriberOnNextListener<RepayPlanRespond>() {
            @Override
            public void onNext(RepayPlanRespond respond) {
                List<RepayPlanRespond.DataBean.RepayRecordsBean> repayRecords = respond.getData().getRepayRecords();
                RepayPlanAdapter repayPlanAdapter = new RepayPlanAdapter(mContext, repayRecords, R.layout.view_repay_plan_item);
                lvRepayPlan.setAdapter(repayPlanAdapter);
            }

            @Override
            public void onError(Throwable e) {

            }
        }, hashMap);
    }

    public class RepayPlanAdapter extends CommonAdapter<RepayPlanRespond.DataBean.RepayRecordsBean> {

        public RepayPlanAdapter(Context context, List<RepayPlanRespond.DataBean.RepayRecordsBean> listDatas, int layoutId) {
            super(context, listDatas, layoutId);
        }

        @Override
        protected void fillData(ViewHolder holder, int position) {
            TextView tvTime = holder.getView(R.id.tv_time);
            TextView tvRepayMoney = holder.getView(R.id.tv_repay_money);
            TextView tvDesc = holder.getView(R.id.tv_desc);

            tvTime.setText(listDatas.get(position).getRealRepaymentDate());
            tvRepayMoney.setText(listDatas.get(position).getLoanAmount());

            StringBuffer stringBuffer = new StringBuffer();
            if (Float.parseFloat(listDatas.get(position).getCurrentPrincipal()) > 0)
                stringBuffer.append("本金(元):" + listDatas.get(position).getCurrentPrincipal() + " + ");
            stringBuffer.append("利息(元):" + listDatas.get(position).getLoanInterest() + " + ");
            stringBuffer.append("逾期利息(元):" + listDatas.get(position).getLoanInterest());

            tvDesc.setText(stringBuffer);
        }
    }
}
